export default {
	/**
	 * 日期范围工具类
	 */
	startTime(time) {
		const nowTimeDate = new Date(time)
		return new Date(nowTimeDate.setHours(0, 0, 0, 0))
	},
	endTime(time) {
		const nowTimeDate = new Date(time)
		return new Date(nowTimeDate.setHours(23, 59, 59, 999))
	},
	/***
	 * 今天的开始时间
	 */
	getBeginToday() {
		return new Date(new Date(new Date().toLocaleDateString()).getTime());
	},
	getBeginYesterday() {
		return this.startTime(this.getBeginToday() - 24 * 60 * 60 * 1000);
	},
	getEndYesterday() {
		return this.endTime(this.getBeginToday() - 24 * 60 * 60 * 1000);
	},

	getDayStartDate() {
		return new Date(new Date(new Date().toLocaleDateString()).getTime());
	},
	//获取当天结束时间
	getDayEndDate() {
		return new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1);
	},

	//获得本周的开始日期
	getWeekStartDate() {
		var newDate = new Date();//Wed Nov 25 2020 10:19:07 GMT+0800 (中国标准时间)
		var Year = newDate.getFullYear();//2020    获取当前年份   
		var Month = newDate.getMonth() + 1;//11      获取当前月份-1， 范围0~11
		var whichWeekDay = newDate.getDay();//3(第四天)  获取今天是本周的第几天-就是星期几    范围 0~6 对应 星期日到星期六
		var Day = newDate.getDate();//25           获取今天是本月的第几天-就是几号      范围1~31
		var weekBeginDate = new Date(Year, Month - 1, (Day - (whichWeekDay ? whichWeekDay : 7)) + 1);//Wed Nov 23 2020 00:00:00 GMT+0800 (中国标准时间)
		return weekBeginDate;
	},

	getWeekEndDate() {
		var newDate = new Date();//Wed Nov 25 2020 10:19:07 GMT+0800 (中国标准时间)
		var Year = newDate.getFullYear();//2020    获取当前年份   
		var Month = newDate.getMonth() + 1;//11      获取当前月份-1， 范围0~11
		var whichWeekDay = newDate.getDay();//3(第四天)  获取今天是本周的第几天-就是星期几    范围 0~6 对应 星期日到星期六
		var Day = newDate.getDate();//25           获取今天是本月的第几天-就是几号      范围1~31
		var weekEndDate = new Date(Year, Month - 1, Day + (7 - (whichWeekDay ? whichWeekDay : 7)));//Sun Nov 29 2020 00:00:00 GMT+0800
		return weekEndDate;
	},
	//获得本月的开始日期 
	getMonthStartDate() {
		var now = new Date();
		return new Date(new Date(now.setDate(1)).setHours(0, 0, 0, 0));
	},

	//获得本月的结束日期 
	getMonthEndDate() {
		var now = new Date();
		var nextMonth = now.getMonth() + 1;
		// 下一个月第一天的时间戳
		var firstDay = new Date(now.setMonth(nextMonth)).setDate(1);
		return new Date(new Date(firstDay - 1000 * 60 * 60 * 24).setHours(23, 59, 59, 0));
	},

	//获得上月开始时间
	getLastMonthStartDate() {
		var lastMonthDate = new Date(); //上月日期
		lastMonthDate.setDate(1);   //变成这个月一号
		lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
		var lastYear = lastMonthDate.getYear();
		lastYear += (lastYear < 2000) ? 1900 : 0; //
		// console.log('lastYear'+lastYear);
		var lastMonth = lastMonthDate.getMonth();
		var lastMonthStartDate = new Date(lastYear, lastMonth, 1);
		return lastMonthStartDate;
	},
	getLastMonthEndDate() {
		var lastMonthDate = new Date(); //上月日期
		lastMonthDate.setDate(1);   //变成这个月一号
		lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
		var lastYear = lastMonthDate.getYear();
		lastYear += (lastYear < 2000) ? 1900 : 0; //
		// console.log('lastYear'+lastYear);
		var lastMonth = lastMonthDate.getMonth();
		var lastMonthEndDate = new Date(lastYear, lastMonth, this.getMonthDays(lastMonth));
		return lastMonthEndDate;
	},

	//获得某月的天数 可以
	getMonthDays(myMonth) {
		var now = new Date(); //当前日期 
		var nowYear = now.getYear(); //当前年 
		var monthStartDate = new Date(nowYear, myMonth, 1);
		var monthEndDate = new Date(nowYear, myMonth + 1, 1);
		//   console.log('天数'+monthStartDate+'==='+ monthEndDate)
		var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
		//  console.log('天数，数量：'+days);
		return days;
	},

	//获得本季度的开始月份 
	getQuarterStartMonth() {
		var now = new Date(); //当前日期 
		var nowMonth = now.getMonth(); //当前月 
		var nowYear = now.getYear(); //当前年 
		nowYear += (nowYear < 2000) ? 1900 : 0; //
		var quarterStartMonth = 0;
		if (nowMonth < 3) {
			quarterStartMonth = 0;
		}
		if (2 < nowMonth && nowMonth < 6) {
			quarterStartMonth = 3;
		}
		if (5 < nowMonth && nowMonth < 9) {
			quarterStartMonth = 6;
		}
		if (nowMonth > 8) {
			quarterStartMonth = 9;
		}
		return quarterStartMonth;
	},

	//获得本季度的开始日期 
	getQuarterStartDate() {
		var now = new Date(); //当前日期 
		var nowYear = now.getYear(); //当前年 
		nowYear += (nowYear < 2000) ? 1900 : 0; //
		var quarterStartDate = new Date(nowYear, this.getQuarterStartMonth(), 1);
		return quarterStartDate;
	},


	getDateDif(sDate1, sDate2) { //sDate1和sDate2是2006-12-18格式  
		var dateSpan,
			tempDate,
			iDays;
		sDate1 = Date.parse(sDate1);
		sDate2 = Date.parse(sDate2);
		dateSpan = sDate2 - sDate1;
		dateSpan = Math.abs(dateSpan);
		iDays = Math.floor(dateSpan / (24 * 3600 * 1000));
		return iDays
	},

	getDateDifDay(sDate1, sDate2) { //获取两个日期相差的天数
		var dateSpan = sDate2.getTime() - sDate1.getTime();
		var dateSpan = Math.abs(dateSpan);
		var iDays = Math.floor(dateSpan / (24 * 3600 * 1000));
		return iDays
	},

	getDifferDay(num) {
		var today = new Date();
		var nowTime = today.getTime();
		var ms = 24 * 3600 * 1000 * num;
		today.setTime(parseInt(nowTime + ms));
		var oYear = today.getFullYear();
		var oMoth = (today.getMonth() + 1).toString();
		if (oMoth.length <= 1) oMoth = '0' + oMoth;
		var oDay = today.getDate().toString();
		if (oDay.length <= 1) oDay = '0' + oDay;
		return oYear + '-' + oMoth + '-' + oDay;
	}


}


/**
 * 时间日期格式化
 * @param format
 * @returns {*}
 */
export const dateFormat = function (dateObj, format) {
	const date = {
		"M+": dateObj.getMonth() + 1,
		"d+": dateObj.getDate(),
		"h+": dateObj.getHours(),
		"m+": dateObj.getMinutes(),
		"s+": dateObj.getSeconds(),
		"q+": Math.floor((dateObj.getMonth() + 3) / 3),
		"S+": dateObj.getMilliseconds()
	};
	if (/(y+)/i.test(format)) {
		format = format.replace(
			RegExp.$1,
			(dateObj.getFullYear() + "").substr(4 - RegExp.$1.length)
		);
	}
	for (const k in date) {
		if (new RegExp("(" + k + ")").test(format)) {
			format = format.replace(
				RegExp.$1,
				RegExp.$1.length === 1 ?
					date[k] :
					("00" + date[k]).substr(("" + date[k]).length)
			);
		}
	}
	return format;
};
